package com.augmentum.oes.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.augmentum.oes.dao.BaseDao;
import com.augmentum.oes.dao.UserDao;
import com.augmentum.oes.model.User;
import com.augmentum.oes.util.JDBCUtil;

/**
 * deal with some information for user
 */

public class UserDaoImpl extends BaseDao implements UserDao {
    
    @Override
    public User getInfo(String userName) {
        String sql = "SELECT * FROM user WHERE user_name = ?";
        User user = jdbcTemplate.queryForObject(sql, new URowMapper(), userName);
        return user;
    }

    class URowMapper implements RowMapper<User> {
        @Override
        public User mapRow(ResultSet rs, int arg1) throws SQLException {
            User user = new User();
            JDBCUtil.toBean(user, rs);
            return user;
        }
    }

    @Override
    public String getName(int id) {
        String sql = "SELECT user_name FROM user WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, String.class, id);
    }
}
